package com.lwg.datastructure.dynamicPrograming;

/**
 * @author: lwg
 * @Date: 2022/2/20 21:38
 * @Description: 最大子数组和
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
     输入：nums = [-2,1,-3,4,-1,2,1,-5,4]
     输出：6
     解释：连续子数组 [4,-1,2,1] 的和最大，为 6

    分析：利用局部比较  首先得出前i-1段最大的和， 即前i-2的和+nums[i-1] 与nums[i-1] 作比较 谁大留下谁
         即公式dp[i]=Math.max(dp[i-1]+nums[i],nums[i])  或者pre=Math.max(pre+num[i],nums[i])
        然后再与初始化的数据进行比较，留下最大的数
 */
public class MaxChildNumsSum {

    public int maxSubArray(int[] nums) {
        int pre = 0, maxAns = nums[0];
        for (int x : nums) {
            pre = Math.max(pre + x, x);
            maxAns = Math.max(maxAns, pre);
        }
        return maxAns;
    }
}

class MainClass12 {
    public static int[] stringToIntegerArray(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.length() == 0) {
            return new int[0];
        }

        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        for(int index = 0; index < parts.length; index++) {
            String part = parts[index].trim();
            output[index] = Integer.parseInt(part);
        }
        return output;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            int[] nums = stringToIntegerArray(line);

            int ret = new MaxChildNumsSum().maxSubArray(nums);

            String out = String.valueOf(ret);

            System.out.print(out);
        }
    }
}
